﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace Prism.WebBuilder.Lib.CArgs
{
	class CArgsCollection : DynamicObject
	{
		private ConcurrentDictionary<string, object> _args;

		public CArgsCollection()
		{
			_args = new ConcurrentDictionary<string, object>();
		}

		public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
		{
			if (indexes.Length != 1)
				return false;
			_args[indexes[0].ToString()] = value;
			return true;
		}

		public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
		{
			result = _args[indexes[0].ToString()];
			return true;
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			object res;
			if (binder.ReturnType == typeof(bool))
			{
				if (_args.TryGetValue(binder.Name, out res))
				{
					bool boolRes;
					if (bool.TryParse(res.ToString(), out boolRes))
					{
						result = true;
					}
					result = false;
				}
				else
					result = false;
			}
			else if (binder.ReturnType == typeof(string))
			{
				if (_args.TryGetValue(binder.Name, out res))
				{
					result = res.ToString();
				}
				else
					result = string.Empty;
			}
			else if (_args.TryGetValue(binder.Name, out res))
			{
				result = res.ToString();
			}
			else
				result = null;
			return true;
		}

	}
}

